#include<iostream>
#include<pthread.h>
#include<vector>
#include<string> 
#include<unistd.h>
using namespace std;

#define NUM 10

__thread int g_val=100;

struct threadData
{
    string tid;
    string threadname;
};

void InitThreadData(threadData *td,int number)
{
    td->threadname="thread-"+to_string(number);
}
string toHex(pthread_t tid)
{
    char buffer[128];
    snprintf(buffer,sizeof(buffer),"0x%lx",tid);
    return buffer;
}

void *threadRoutine(void *args)
{
    int test_i=0;
    threadData *td=static_cast<threadData*> (args);
    td->tid=toHex(pthread_self());
    int i=0;
    while(i==0)
    {
        cout<<"pid: "<<getpid()<<", tid: "<<td->tid<<", threadname: "<<td->threadname<<", test_i: "
        <<test_i<<", &test_i: "<<toHex((pthread_t)&test_i)<<endl;
        sleep(1);
        i++;
        test_i++;
    }
    delete td;
    return nullptr;
}
int main()
{
    //创建多线程
    vector<pthread_t> tids;
    for(int i=0;i<NUM;i++)
    {
        pthread_t tid;
        threadData *td=new threadData;
        InitThreadData(td,i);

        pthread_create(&tid,nullptr,threadRoutine,(void*)td);
        tids.push_back(tid);
        sleep(1);
    }
    for(int i=0;i<tids.size();i++)
    {
        pthread_join(tids[i],nullptr);
    }
    return 0;
}